package com.kg.sdk.activity;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.common.network.NetworkConfigData;
import com.common.network.NetworkResponse;
import com.common.network.RequestListener;
import com.kg.sdk.KPGameData;
import com.kg.sdk.KPGameSdk;
import com.kg.sdk.SharedPreferencesCtrl;
import com.kg.sdk.adapt.KPGiftAdapter;
import com.kg.sdk.adapt.KPGiftData;
import com.kg.sdk.dlg.KPDlgTip;
import com.kg.sdk.network.KPPostRequest;
import com.kg.sdk.util.CommonUtil;
import com.kg.sdk.util.Helper;
import com.kg.sdk.util.KPNetworkData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class KPUserCenterActivity extends KPCommonActivity implements View.OnClickListener,KPGiftAdapter.GiftClickListener {
    String auth_code = "";
    String auth_key = "";
    private TextView tvUserName;
    private TextView tvMoney;
    private TextView tvCharge;
    private ImageView imgKf;
    private String account = "";
    private String surplus = "";
    private ListView giftList;
    private int game_id = -1;
    KPDlgTip mKPDlgTip;
    List<KPGiftData> giftDataList = new ArrayList<KPGiftData>();
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        int layoutId = Helper.getLayoutResId(this,"act_user_center_layout");
        View view = LayoutInflater.from(this).inflate(layoutId, null);
        setContentView(view);
        SharedPreferencesCtrl sharedPreferencesCtrl = new SharedPreferencesCtrl(this,KPNetworkData.KPGAME);
        auth_code = sharedPreferencesCtrl.getString(KPNetworkData.AUTH_CODE_KYE,"");
        auth_key = sharedPreferencesCtrl.getString(KPNetworkData.AUTH_KYE,"");

        View imgBack = findViewById(Helper.getIdResId(this,"imgBack"));
        giftList = (ListView)findViewById(Helper.getIdResId(this,"giftList"));
        giftList.setCacheColorHint(0);
        imgBack.setOnClickListener(this);

        Button btnUserCenter = (Button)findViewById(Helper.getIdResId(this,"btnUserCenter"));
        btnUserCenter.setOnClickListener(this);

        Button btnUserCenter1 = (Button)findViewById(Helper.getIdResId(this,"btnUserCenter1"));
        btnUserCenter1.setOnClickListener(this);

        Button btnUserCenter2 = (Button)findViewById(Helper.getIdResId(this,"btnUserCenter2"));
        btnUserCenter2.setOnClickListener(this);

        Button btnUserCenter3 = (Button)findViewById(Helper.getIdResId(this,"btnUserCenter3"));
        btnUserCenter3.setOnClickListener(this);
        Button btnUserCenter4 = (Button)findViewById(Helper.getIdResId(this,"btnUserCenter4"));
        btnUserCenter4.setOnClickListener(this);

        imgKf = (ImageView)findViewById(Helper.getIdResId(this,"imgKf"));
        imgKf.setOnClickListener(this);

        tvUserName = (TextView)findViewById(Helper.getIdResId(this,"tvUserName"));
        tvMoney = (TextView)findViewById(Helper.getIdResId(this,"tvMoney"));
        tvCharge = (TextView)findViewById(Helper.getIdResId(this,"tvCharge"));
        tvCharge.setOnClickListener(this);
        KPGameData kpGameData = KPGameSdk.getGameData(this);
        game_id = kpGameData.game_id;
        if(game_id == -1){
            CommonUtil.showToast(this,"请先配置游戏id");
        }
        mWaitingDialog.showDialog();
        getUseInfo();
        test1();
        KPGameSdk.hideFloatView(this);
        mKPDlgTip = new KPDlgTip(this);
    }

    private void startCharge(){
        Intent intent = new Intent();
        intent.putExtra(KPNetworkData.ACCOUNT,account);
        intent.putExtra(KPNetworkData.SURPLUS,surplus);
        intent.setClass(KPUserCenterActivity.this, KPChargeActivity.class);
        startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();

        if(id == Helper.getIdResId(this,"imgBack")){
            finish();
            return;

        }else if(id == Helper.getIdResId(this,"tvCharge")){
            startCharge();

        }else if(id == Helper.getIdResId(this,"btnUserCenter") ){
            getUseInfo();

        }else if(id == Helper.getIdResId(this,"btnUserCenter1")){
            test1();
        }else if(id == Helper.getIdResId(this,"btnUserCenter4")){
            test4();
        }else if(id == Helper.getIdResId(this,"imgKf")){

            startCharge();
        }
        else {
            test2();
        }

    }

    private void getUseInfo(){

        String url = KPNetworkData.USER_CENTER;
        RequestListener listener = new RequestListener() {
            @Override
            public void onSuccess(NetworkResponse result) {
                int code = result.getInt(KPNetworkData.ERROR_CODE);
                final String data = result.getString(NetworkConfigData.DATA_KEY);
                Log.e("data"," usce center logni success is "+data);
                if(code == KPNetworkData.SUCCESS){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jsonObject = new JSONObject(data);
                                account = jsonObject.getString(KPNetworkData.ACCOUNT);
                                tvUserName.setText(account);
                                surplus = jsonObject.getString(KPNetworkData.SURPLUS);
                                tvMoney.setText(surplus);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
                }else{
                    final String errorMsg = result.getString(KPNetworkData.MSG_KEY);
                    Log.e("data"," login errorMsg 11 is "+errorMsg);
                    showError(errorMsg);
                }
            }

            @Override
            public void onError(NetworkResponse error) {
                final String errorMsg = error.getString(NetworkConfigData.MESSAGE_KEY);
                Log.e("data"," login errorMsg is "+errorMsg);
                showError(errorMsg);
            }
        };
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(KPNetworkData.ACT_KEY,"index");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        request(KPNetworkData.USER_CENTER,jsonObject,listener);

    }

    private void test1(){

        String url = KPNetworkData.USER_CENTER;
        RequestListener listener = new RequestListener() {
            @Override
            public void onSuccess(NetworkResponse result) {
                int code = result.getInt(KPNetworkData.ERROR_CODE);
                String data = result.getString(NetworkConfigData.DATA_KEY);
                Log.e("data"," usce center1 logni success is "+data);
                if(code == KPNetworkData.SUCCESS){
                    try {
                        JSONArray jsonArray = new JSONArray(data);
                        for(int i = 0;i < jsonArray.length();i++){
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            KPGiftData giftData = new KPGiftData(jsonObject);
                            giftDataList.add(giftData);
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mWaitingDialog.closeDialog();
                                KPGiftAdapter adapter = new KPGiftAdapter(KPUserCenterActivity.this,giftDataList);
                                adapter.setGiftClickListener(KPUserCenterActivity.this);
                                giftList.setAdapter(adapter);
                            }
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else{
                    final String errorMsg = result.getString(KPNetworkData.MSG_KEY);
                    showError(errorMsg);
                }
            }

            @Override
            public void onError(NetworkResponse error) {
                final String errorMsg = error.getString(NetworkConfigData.MESSAGE_KEY);
                Log.e("data"," login errorMsg is "+errorMsg);
                showError(errorMsg);
            }
        };
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(KPNetworkData.ACT_KEY,"gifts");
          //  jsonObject.put("game_id",game_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        request(KPNetworkData.USER_CENTER,jsonObject,listener);
    }

    private void test2(){

        String url = KPNetworkData.USER_CENTER;
        RequestListener listener = new RequestListener() {
            @Override
            public void onSuccess(NetworkResponse result) {
                int code = result.getInt(KPNetworkData.ERROR_CODE);
                String data = result.getString(NetworkConfigData.DATA_KEY);
                Log.e("data"," usce center2 logni success is "+data);
                if(code == KPNetworkData.SUCCESS){

                }else{
                    final String errorMsg = result.getString(KPNetworkData.MSG_KEY);
                    showError(errorMsg);
                }
            }

            @Override
            public void onError(NetworkResponse error) {
                final String errorMsg = error.getString(NetworkConfigData.MESSAGE_KEY);
                Log.e("data"," login errorMsg is "+errorMsg);
                showError(errorMsg);
            }
        };
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(KPNetworkData.ACT_KEY,"server");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        request(KPNetworkData.USER_CENTER,jsonObject,listener);

    }
     String gift_code = "";
    private void getGift(int giftId){

        String url = KPNetworkData.USER_CENTER;
        RequestListener listener = new RequestListener() {
            @Override
            public void onSuccess(NetworkResponse result) {
                int code = result.getInt(KPNetworkData.ERROR_CODE);
                String data = result.getString(NetworkConfigData.DATA_KEY);
                Log.e("data"," usce center2 logni success is "+data);
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    gift_code = jsonObject.getString("gift_code");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if(code == KPNetworkData.SUCCESS){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            CommonUtil.copy(KPUserCenterActivity.this,gift_code);
                            mWaitingDialog.closeDialog();
                            if(mKPDlgTip != null){
                                mKPDlgTip.showDialog();
                            }
                           // CommonUtil.showToast(KPUserCenterActivity.this,"礼包码领取并复制成功");
                        }
                    });
                }else{
                    final String errorMsg = result.getString(KPNetworkData.MSG_KEY);
                    showError(errorMsg);
                }
            }

            @Override
            public void onError(NetworkResponse error) {
                final String errorMsg = error.getString(NetworkConfigData.MESSAGE_KEY);
                Log.e("data"," login errorMsg is "+errorMsg);
                showError(errorMsg);
            }
        };
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(KPNetworkData.ACT_KEY,"draw");
            jsonObject.put("gift_id",giftId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        request(KPNetworkData.USER_CENTER,jsonObject,listener);

    }

    private void test4(){


        RequestListener listener = new RequestListener() {
            @Override
            public void onSuccess(NetworkResponse result) {
                int code = result.getInt(KPNetworkData.ERROR_CODE);
                String data = result.getString(NetworkConfigData.DATA_KEY);
                Log.e("data"," usce center2 logni success is "+data);
                if(code == KPNetworkData.SUCCESS){

                }else{
                    final String errorMsg = result.getString(KPNetworkData.MSG_KEY);
                    showError(errorMsg);
                }
            }

            @Override
            public void onError(NetworkResponse error) {
                final String errorMsg = error.getString(NetworkConfigData.MESSAGE_KEY);
                showError(errorMsg);
                Log.e("data"," login errorMsg is "+errorMsg);
            }
        };
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("ordersn","2017071017283610159");
            jsonObject.put("pay_type",3);
          //  jsonObject.put("game_id",game_id);
            jsonObject.put("product","首充");
            jsonObject.put("role_id","1000");
            jsonObject.put("amount","6");
            jsonObject.put("server_id","server001");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        request(KPNetworkData.USER_CHARGE,jsonObject,listener);
    }
    private void request(String url,JSONObject jsonObject,RequestListener listener){
        KPPostRequest postRequest = new KPPostRequest(this,url,jsonObject.toString(),listener);
        postRequest.header("auth_code",auth_code);
        postRequest.header("auth_key",auth_key);
        postRequest.startAsync();
    }

    private void showError(final String error){

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mWaitingDialog.closeDialog();
                CommonUtil.showToast(KPUserCenterActivity.this,error);
            }
        });
    }

    @Override
    public void onGiftClick(int position) {
        mWaitingDialog.showDialog();
        KPGiftData data = giftDataList.get(position);
        getGift(data.id);
    }

    public void onDestroy(){
        super.onDestroy();
        if(KPGameSdk.isLogin(this) && KPGameSdk.checkFloatPermission1(this)){//判断是否登录过
            KPGameSdk.showFloatView2(this);//启动悬浮框
        }
        if(mKPDlgTip != null){
            mKPDlgTip.closeDialog();
        }
    }
}
